package com.wanshi.dao;

import com.wanshi.entity.CartItem;
import com.wanshi.entity.Order;
import com.wanshi.entity.OrderItem;
import com.wanshi.utils.JDBCUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDao {

    //1 添加记录
    public void addOrder(Connection connection,Order order) throws SQLException {
        QueryRunner runner = new QueryRunner();
       /* id varchar(50) NOT NULL // uuid
        money double NULL         //money  算出来
        paystate int(11) NULL(0未支付，1已支付，2 删除) //0
        receinfo varchar(80) NULL    //address
        add_time timestamp NOT NULL  // null
        user_id int(11) NULL    // 可以从session中获取
        state int(5) NULL*/
        String sql = "insert into orders values(?,?,?,?,null,?,?)";
            runner.update(connection,sql,order.getId(),order.getMoney(),order.getPaystate(),
                    order.getReceinfo(),order.getUser_id(),order.getState());
    }

    public void addOrderItem(Connection conn,String orderId,String prodid, int buynum) throws SQLException {
        QueryRunner runner = new QueryRunner();
        String sql = "insert into orderitem values(?,?,?)";
        runner.update(conn,sql,orderId,prodid,buynum);
    }


    public List<Order> findOrdersByUser(int userid) {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "SELECT * FROM orders WHERE user_id = ? and state = 0";

        try {
            return runner.query(sql,new BeanListHandler<>(Order.class),userid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<OrderItem> findOrderItemsByOrderId(String orderid) {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "SELECT * FROM orderitem WHERE order_id = ?";

        try {
            return runner.query(sql,new BeanListHandler<>(OrderItem.class),orderid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public List<OrderItem> findOrderItemsByOrderId(Connection conn,String orderid) {
        QueryRunner runner = new QueryRunner();
        String sql = "SELECT * FROM orderitem WHERE order_id = ?";

        try {
            return runner.query(conn,sql,new BeanListHandler<>(OrderItem.class),orderid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void delItemsByOId(Connection conn,String orderid) throws SQLException {
        QueryRunner runner = new QueryRunner();
        String sql = "delete from orderitem where order_id = ?";
        runner.update(conn,sql,orderid);
    }

    public void delOrderById(Connection conn,String orderid) throws SQLException {
        QueryRunner runner = new QueryRunner();
        String sql = "delete from orders where id = ?";
        runner.update(conn,sql,orderid);
    }

    public void updateOrderById(String orderid) {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "update orders set state = -1 where id = ?";
        try {
            runner.update(sql,orderid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Order findOrderByOId(String orderid) {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "SELECT * FROM orders WHERE id = ?";
        try {
            return runner.query(sql,new BeanHandler<>(Order.class),orderid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void updatePayStateByOId(String orderid) {
        QueryRunner runner = new QueryRunner(JDBCUtil.getSource());
        String sql = "update orders set paystate = 1 where id = ?";
        try {
            runner.update(sql,orderid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
